<?php
declare(strict_types=1);

namespace SixShop\Message\Controller\Admin;

use think\Request;

/**
 * 系统公告后台控制器
 */
class AnnouncementController extends BaseController
{
    /**
     * 默认入口方法（兼容资源路由）
     */
    public function index()
    {
        return $this->getList(request());
    }
    
    /**
     * 获取公告列表
     */
    public function getList(Request $request)
    {
        $page = $request->param('page/d', 1);
        $limit = $request->param('limit/d', 10);
        $title = $request->param('title', '');
        $status = $request->param('status', '');
        $startTime = $request->param('start_time', '');
        $endTime = $request->param('end_time', '');
        
        $params = [];
        if (!empty($title)) {
            $params['title'] = $title;
        }
        if ($status !== '') {
            $params['status'] = $status;
        }
        if (!empty($startTime)) {
            $params['start_time'] = $startTime;
        }
        if (!empty($endTime)) {
            $params['end_time'] = $endTime;
        }
        
        $result = $this->announcementModel->getList($params, $page, $limit);
        
        return $this->success('获取成功', $result);
    }
    
    /**
     * 获取公告详情
     */
    public function getDetail(Request $request)
    {
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $detail = $this->announcementModel->getDetail($id);
        if (!$detail) {
            return $this->error('公告不存在');
        }
        
        return $this->success('获取成功', $detail);
    }
    
    /**
     * 添加公告
     */
    public function save(Request $request)
    {
        $title = $request->param('title', '');
        $content = $request->param('content', '');
        $type = $request->param('type/d', 1);
        $status = $request->param('status/d', 1);
        $startTime = $request->param('start_time', '');
        $endTime = $request->param('end_time', '');
        $sort = $request->param('sort/d', 100);
        
        if (empty($title)) {
            return $this->error('标题不能为空');
        }
        
        if (empty($content)) {
            return $this->error('内容不能为空');
        }
        
        $data = [
            'title' => $title,
            'content' => $content,
            'type' => $type,
            'status' => $status,
            'sort' => $sort,
        ];
        
        if (!empty($startTime)) {
            $data['start_time'] = strtotime($startTime);
        }
        
        if (!empty($endTime)) {
            $data['end_time'] = strtotime($endTime);
        }
        
        $result = $this->announcementModel->add($data);
        if (!$result) {
            return $this->error('添加失败');
        }
        
        return $this->success('添加成功', ['id' => $result]);
    }
    
    /**
     * 更新公告
     */
    public function update(Request $request)
    {
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $title = $request->param('title', '');
        $content = $request->param('content', '');
        $type = $request->param('type/d', null);
        $status = $request->param('status/d', null);
        $startTime = $request->param('start_time', '');
        $endTime = $request->param('end_time', '');
        $sort = $request->param('sort/d', null);
        
        $data = [];
        
        if (!empty($title)) {
            $data['title'] = $title;
        }
        
        if (!empty($content)) {
            $data['content'] = $content;
        }
        
        if ($type !== null) {
            $data['type'] = $type;
        }
        
        if ($status !== null) {
            $data['status'] = $status;
        }
        
        if ($sort !== null) {
            $data['sort'] = $sort;
        }
        
        if (!empty($startTime)) {
            $data['start_time'] = strtotime($startTime);
        }
        
        if (!empty($endTime)) {
            $data['end_time'] = strtotime($endTime);
        }
        
        if (empty($data)) {
            return $this->error('没有需要更新的数据');
        }
        
        $result = $this->announcementModel->update($id, $data);
        if (!$result) {
            return $this->error('更新失败');
        }
        
        return $this->success('更新成功');
    }
    
    /**
     * 删除公告
     */
    public function delete(Request $request)
    {
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $result = $this->announcementModel->delete($id);
        if (!$result) {
            return $this->error('删除失败');
        }
        
        return $this->success('删除成功');
    }
    
    /**
     * 批量删除公告
     */
    public function batchDelete(Request $request)
    {
        $ids = $request->param('ids');
        if (empty($ids)) {
            return $this->error('参数错误');
        }
        
        if (!is_array($ids)) {
            $ids = explode(',', $ids);
        }
        
        $result = $this->announcementModel->batchDelete($ids);
        if (!$result) {
            return $this->error('删除失败');
        }
        
        return $this->success('删除成功');
    }
    
    /**
     * 更新公告状态
     */
    public function updateStatus(Request $request)
    {
        $id = $request->param('id/d', 0);
        $status = $request->param('status/d', 0);
        
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $result = $this->announcementModel->update($id, ['status' => $status]);
        if (!$result) {
            return $this->error('操作失败');
        }
        
        return $this->success('操作成功');
    }
}
